<?php

/**
 * 处理用户信息函数库
 * --------------------------------------------------------
 * @copyright  http://lin.97md.net
 * @access       MyDream
 * @author      zxing@97md.net
 * $Id: common.fun.php 29 2011-12-09 07:08:05Z thesmilelin@gmail.com $
 * $Date: 2011-12-09 15:08:05 +0800 (星期五, 2011-12-09) $
 * --------------------------------------------------------
 * @example
 */
//处理用户提交的信息(添加新用户)
function submit_user($username='', $password='', $password1='', $email='') {
    global $_DBLINK;

    $name = empty($username) ? '' : $username;
    $pass = empty($password) ? '' : $password;
    $pass1 = empty($password1) ? '' : $password1;
    $ema = empty($email) ? '' : $email;
    //若提交的所有信息均为空，则出现警告，并重新回到提交页面
    if (empty($name) && empty($pass) && empty($pass1) && empty($ema)) {
        show_msg('请输入信息后再提交', '/admin/user.php?action=add');
    }
    //添加管理员的编号()`add_admin`
    $sql1 = 'SELECT `add_admin` FROM `users` ORDER BY `id` DESC LIMIT 0,1';
    $result1 = mysql_query($sql1);
    $pids = mysql_fetch_row($result1);
    $pid = $pids[0] + 1;

    $sql = 'INSERT INTO `users`(`login_name`,`password`,`email`,`add_time`,`add_admin`) VALUES(\'' . $name . '\',\'' . password($pass) . '\',\'' . $ema . '\',\'' . time() . '\',\'' . $pid . '\')';
    // $sql = 'INSERT INTO `users`(`login_name`,`password`,`email`,`add_time`) VALUES(\'' . $name . '\',\'' . $pass . '\',\'' . $ema . '\',\'' . time() . '\')';
    $result = mysql_query($sql);

    if (mysql_affected_rows($_DBLINK) > 0) {
        show_msg('恭喜您！添加用户成功 ', '/admin/user.php?action=default');
    }
}

//显示除密码外用户信息
function manuser() {
    //读取用户的信息：login_name email add_time 
    $sql = 'SELECT `login_name`,`email`,`add_time` ,`id` FROM `users`';
    $result = mysql_query($sql);

    while ($rows = mysql_fetch_row($result)) {
        $uid = $rows[3];
        $rows[2] = date('Y-m-d H:i:s', $rows[2]);
        $row = array($rows[0], $rows[1], $rows[2]);
        $return.='<div class=\'rows\'>';
        foreach ($row as $value) {
            $return.='<div class=\'cols\'>';
            $return.=$value;
            $return.='</div>';
        }
        $return.='<div class="cols">';
        $return.=maner($uid);
        $return.='</div>';
        $return.='</div>';
        $return.='<div class=\'ro\'></div>';
    }
    mysql_free_result($result);

    return $return;
}

//为每条用户信息，加上相关操作
function maner($uid) {
    $man = "<a href='user.php?action=edit&uid=" . $uid . "'  >[修改]</a>&nbsp;";
    $man .= "<a href='user.php?action=del&uid=" . $uid . "'  >[删除]</a>";
    return $man;
}

//根据用户id获取某个用户的信息
function get_msg($uid) {
    $sql_uid = 'SELECT `login_name`,`password`,`email` FROM `users` WHERE `id`=' . $uid . '';
    $result_uid = mysql_query($sql_uid);
    while ($datas = mysql_fetch_row($result_uid)) {
        $userInfo = array();
        $userInfo['username'] = $datas[0];
       // $userInfo['password'] = '123456';
        $userInfo['email'] = $datas[2];
    }


    return $userInfo;
}

function edit_user($username='', $password='', $password1='', $email='', $uid='') {
    global $_DBLINK;
    $name = empty($username) ? '' : $username;
    $pass = empty($password) ? '' : $password;
    $pass1 = empty($password1) ? '' : $password1;
    $ema = empty($email) ? '' : $email;
    //若提交的所有信息均为空，则出现警告，并重新回到提交页面
    if (empty($name) && empty($pass) && empty($pass1) && empty($ema)) {
        show_msg('请输入信息后再提交', '/admin/user.php?action=add');
        exit;
    }
    $sql_asub = 'UPDATE `users` SET `login_name`=\'' . $name . '\',`password`=\'' . password($pass) . '\',`email`=\'' . $ema . '\',`add_time`=\'' . time() . '\' WHERE `id`=\'' . $uid . '\'';
    $result_asub = mysql_query($sql_asub);

    if (mysql_affected_rows($_DBLINK) > 0) {
        show_msg('恭喜您！用户信息修改成功 ', '/admin/user.php?action=default');
    } else {
        show_msg('用户信息修改失败 ', '/admin/user.php?action=default');
    }
}

function del_user($uid) {    //删除用户的方法
    global $_DBLINK;
    $sql = 'DELETE FROM `users` WHERE `id`=' . $uid . '';
    mysql_query($sql);

    if (mysql_errno()) {
        echo 'cuowu :' . mysql_error();
    }
    if (mysql_affected_rows($_DBLINK) > 0) {
        show_msg('用户删除成功！', '/admin/user.php?action=default');
    } else {
        show_msg('用户删除失败！', '/admin/user.php?action=default');
    }
}

